home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
mflms101.arc
/
WC.C
< prev
Wrap
C/C++ Source or Header
|
1989-11-25
|
6KB
|
186 lines
/*
** WORD COUNT UTILITY
**
** syntax:
** wc [-cwlsv] file
** -c count only characters
** -w count only words
** -l count only lines
** -s gives only a hex checksum
** -q silences reporting of filenames
** file is input file (Do NOT use stdin redirection!)
** options must be first argument only
** wildcard filenames are allowed
**
** ========== COPYRIGHT 1988 BY STEVEN E. MARGISON ==============
** 11-08-88 A Turbo-C 2.0
**
** Modified 1989 by Bob Stout
**
** As distributed, this program requires (for compilation):
** "The MicroFirm Function LIbrary for MS/QC"
** which may be obtained without registration from many Bulletin
** Board Systems.
**
** or by registration:
** $25 for Docs, C, S, M, L, H libraries, and complete library source
** in C and Assembler
** MicroFirm
** P.O. Box 428
** Alief, TX 77411
**
**
*/
#include <stdio.h>
#include <ctype.h>
#include <mflfiles.h>
int binflg, copt, wopt, lopt, sopt, allopt, verbose;
FILE *fd;
int nargc;
main(argc, argv)
int argc;
char *argv[];
{
char *cksum, *nc, *nl, *nw;
int index, c, inword, argci;
if (argc < 2)
usage();
nargc = expand_args(argc, argv); /* expand wildcards */
if (nargc == 0) error("Error in command line expansion");
verbose = TRUE;
sopt = lopt = wopt = copt = FALSE;
allopt=TRUE;
argci = 1;
if (nargv[argci][0] == '-')
{
index = 0;
while (nargv[argci][++index] != NUL)
{
switch(tolower(nargv[argci][index]))
{
case 'q':
verbose = FALSE;
break;
case 'c':
copt = TRUE;
allopt = FALSE;
break;
case 'w':
wopt = TRUE;
allopt = FALSE;
break;
case 'l':
lopt = TRUE;
allopt = FALSE;
break;
case 's':
sopt = TRUE;
allopt = FALSE;
break;
default:
usage();
} /* end of switch */
} /* end of while */
argci++; /* get around options */
} /* end of option "if" */
while (argci < nargc)
{
cksum = nc = nl = nw = 0;
inword = FALSE;
if (nargv[argci][0] == '-')
usage();
do_open(nargv[argci++]);
while ((c = fgetc(fd)) != EOF)
{
cksum += c;
if (binflg)
continue;
if (c == '\n')
{
++nl;
++nc;
++nc; /* because a newline is actually two characters */
inword = FALSE;
}
else ++nc;
if (isspace(c))
inword = FALSE;
else if (inword == FALSE)
{
inword = TRUE;
++nw;
}
} /* end of inner while */
if (allopt && !binflg)
printf("%6d characters %d words %d lines %6x checksum\n",
nc, nw, nl, cksum);
if (binflg)
printf("%x checksum\n", cksum);
if (copt)
printf("%d\n", nc);
if (wopt)
printf("%d\n", nw);
if (lopt)
printf("%d\n", nl);
if (sopt)
printf("%x\n", cksum);
fclose(fd);
} /* end of file loop */
}
usage()
{
fputs("WC Version 1.51 11-08-88 Copyright 1988 S.E. Margison\n", stderr);
error("usage: wc [-cwlsq] <file1, file2, filen>");
}
do_open(char *string)
{
binflg = FALSE;
if (exttyp(string, "OBJ") == TRUE)
{
binflg = TRUE;
goto AA;
}
if (exttyp(string, "EXE") == TRUE)
{
binflg = TRUE;
goto AA;
}
if (exttyp(string, "COM") == TRUE)
{
binflg = TRUE;
goto AA;
}
if (exttyp(string, "ARC") == TRUE)
{
binflg = TRUE;
goto AA;
}
if (exttyp(string, "LIB") == TRUE)
binflg = TRUE;
AA: if (!binflg)
{
if ((fd = fopen(string, "r")) == NULL)
cant(string);
}
else
{
if ((fd = fopen(string, "rb")) == NULL)
cant(string);
}
if (verbose) printf("File: %s\n", string);
}